package com.wuseguang.springcloud.Classcardservice.impl;


import com.wuseguang.springcloud.Classcardservice.ClassCardFieldService;
import com.wuseguang.springcloud.JdbcTemplate.ClassCardDao;
import com.wuseguang.springcloud.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class ClassCardFieldImpl implements ClassCardFieldService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ClassCardDao classCardDao;

    /**
     * @描述 修改心跳接口
     * @创建人 zm
     * @创建时间 2020/1/16
     */
    @Override
    public void pushClassCard(String id, String type, String flag) {
        if (id == null || "".equals(id)) {
            return;
        }
        String sql = " UPDATE bancard SET " + type + "=? WHERE id IN (" + id + ")";
        classCardDao.updateByParameter(sql, flag);
        logger.info("pushClassCard-->修改心跳={},修改={}，执行完毕.....", type, flag);
    }


    @Override
    public void pushClassCard(Integer id, String type, Integer flag) {
        if (id == null || "".equals(id)) {
            return;
        }
        String sql = " UPDATE bancard SET " + type + "=? WHERE id =?";
        classCardDao.updateByParameter(sql, flag, id);
        logger.info("pushClassCard-->修改心跳={},修改={}，执行完毕.....", type, flag);
    }


    private String iterList(List<Integer> list) {
        logger.info("iterList==>list={}", list.toString());
        if (list == null || list.size() <= 0) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            stringBuilder.append(list.get(i)).append(",");
        }
        String substring = stringBuilder.substring(0, stringBuilder.length() - 1);
        logger.info("iterList==>substring={}", substring);
        return substring;
    }

    private String iterStringList(List<String> list) {
        logger.info("iterStringList==>list={}", list.toString());
        if (list == null || list.size() <= 0) {
            return null;
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            stringBuilder.append(list.get(i)).append(",");
        }
        String substring = stringBuilder.substring(0, stringBuilder.length() - 1);
        logger.info("iterStringList==>substring={}", substring);
        return substring;
    }

    /**
     * @描述 单个班级，修改心跳
     * @创建人 zm
     * @创建时间 2020/1/16
     */
    @Override
    public void classesAndCards(String classid, String type, String flag) {
        if (classid == null || "".equals(classid)) {
            return;
        }
        String sql = "SELECT GROUP_CONCAT(b.id) as string FROM classroom r,classes s ,bancard b WHERE r.classid=s.id AND b.classroomid=r.id AND r.classid=?";
        String s = classCardDao.queryString(sql, classid);
        pushClassCard(s, type, flag);
    }

    @Override
    public void classesAndCardsMultiple(String classid, String type, String flag) {
        if (classid == null || "".equals(classid)) {
            return;
        }
        String sql = "SELECT GROUP_CONCAT(b.id) as string FROM classroom r,classes s ,bancard b WHERE r.classid=s.id AND b.classroomid=r.id AND r.classid  in (" + classid + ")";
        String s = classCardDao.queryString(sql);
        pushClassCard(s, type, flag);
    }


    @Override
    public void classesAndCards(Integer classid, String type, String flag) {
        if (classid == null || "".equals(classid)) {
            return;
        }
        String sql = "SELECT GROUP_CONCAT(b.id) as string FROM classroom r,classes s ,bancard b WHERE r.classid=s.id AND b.classroomid=r.id AND r.classid=?";
        String s = classCardDao.queryString(sql, classid);
        pushClassCard(s, type, flag);
    }

    /**
     * @描述 多个班级，修改心跳
     * @创建人 zm
     * @创建时间 2020/1/16
     */
    @Override
    public void classesAndCards(List<Integer> classids, String type, String flag) {
        String s = iterList(classids);
        if (s == null) {
            return;
        }
        String sql = "SELECT GROUP_CONCAT(b.id) as string FROM classroom r,classes s ,bancard b WHERE r.classid=s.id AND b.classroomid=r.id AND r.classid in (" + s + ")";
        String s1 = classCardDao.queryString(sql);
        pushClassCard(s1, type, flag);
    }

    /**
     * @描述 单个教室，修改心跳
     * @创建人 zm
     * @创建时间 2020/1/16
     */
    @Override
    public void classRoomAndCards(String classRoomid, String type, String flag) {
        if (classRoomid == null || "".equals(classRoomid)) {
            return;
        }
        String sql = "SELECT GROUP_CONCAT(b.id) as string FROM classroom r,bancard b WHERE  b.classroomid=r.id AND r.id=?";
        String s1 = classCardDao.queryString(sql, classRoomid);
        pushClassCard(s1, type, flag);
    }

    /**
     * @描述 多个教室，修改心跳
     * @创建人 zm
     * @创建时间 2020/1/16
     */
    @Override
    public void classRoomAndCards(List<Integer> classRoomids, String type, String flag) {
        String s = iterList(classRoomids);
        if (s == null) {
            return;
        }
        String sql = "SELECT GROUP_CONCAT(b.id) as string FROM classroom r,bancard b WHERE  b.classroomid=r.id AND r.id in (" + s + ")";
        String s1 = classCardDao.queryString(sql);
        pushClassCard(s1, type, flag);
    }

    /**
     * @描述 单个设备id，修改心跳
     * @创建人 zm
     * @创建时间 2020/1/16
     */
    @Override
    public void bancardAndCards(String bancardid, String type, String flag) {
        pushClassCard(bancardid, type, flag);
    }

    /**
     * @描述 多个设备ids，修改心跳
     * @创建人 zm
     * @创建时间 2020/1/16
     */
    @Override
    public void bancardAndCards(List<Integer> bancardids, String type, String flag) {
        String s = iterList(bancardids);
        if (s == null) {
            return;
        }
        pushClassCard(s, type, flag);
    }

    /**
     * @描述 单个设备，修改心跳
     * @创建人 zm
     * @创建时间 2020/1/16
     */
    @Override
    public void deviceidAndCards(String deviceid, String type, String flag) {
        String sql = "SELECT GROUP_CONCAT(b.id) as string FROM classroom r,bancard b WHERE b.classroomid=r.id AND b.deviceid=?";
        String s1 = classCardDao.queryString(sql, deviceid);
        pushClassCard(s1, type, flag);
    }

    /**
     * @描述 多个设备，修改心跳
     * @创建人 zm
     * @创建时间 2020/1/16
     */
    @Override
    public void deviceidAndCards(List<String> deviceids, String type, String flag) {
        String s = iterStringList(deviceids);
        if (s == null) {
            return;
        }
        String sql = "SELECT GROUP_CONCAT(b.id) as string FROM classroom r,bancard b WHERE b.classroomid=r.id AND b.deviceid in (" + StringUtil.spilt(s) + ")";
        String s1 = classCardDao.queryString(sql);
        pushClassCard(s1, type, flag);
    }

}
